package cn.schoolwow.quickdao.module.database.dml.instance.flow.common;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class SetInstanceEntityFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        Object instance = flowContext.getData("instance");
        if(null==instance){
            Object[] instances = (Object[]) flowContext.checkData("instances");
            instance = instances[0];
        }
        Entity entity = quickDAOConfig.getEntityByClassName(instance.getClass().getName());
        if(null==entity){
            throw new IllegalArgumentException("根据实例类名获取实体类信息失败!实例类名:"+instance.getClass().getName());
        }
        flowContext.putTemporaryData("entity", entity);
    }

    @Override
    public String name() {
        return "设置实例Entity信息";
    }
}
